<template>
  <div>
    <div>&nbsp;</div>
    <div>&nbsp;</div>
    <h1>{{ 'Test Model' }}</h1>

    <div>&nbsp;</div>
    <button @click="onclick"><H1> Form View</H1></button>

    <div>
      <h6>详情页面</h6>

      <!-- image_1920 -->
      <!--  /web/image?model=xxx&id=1&field=image_1920&unique=123123113 -->

      <div>name:{{ dataDict.name }}</div>
      <div>display_name: {{ dataDict.display_name }}</div>
      <div>
        type: {{ dataDict.type }}
        {{ format_selection('type') }}
      </div>
      <div>barcode: {{ dataDict.barcode }}</div>
      <div>default_code: {{ dataDict.default_code }}</div>

      <div>
        <div>详情页面, 对 m2o 字段的显示处理.</div>
        categ_id: 文本为=
        {{ (dataDict.categ_id || [0, null])[1] }}
        id是={{ (dataDict.categ_id || [0, null])[0] }} 可用于超链接的参数
      </div>
    </div>

    <div>&nbsp;</div>
    <div>&nbsp;</div>

    <div>&nbsp;</div>
    <button @click="onclickLogin"><H1>Login</H1></button>

    <div>
      <h2>-- session_info.server_version---</h2>
      {{ session_info.server_version }}

      <h2>-- session_info.uid---</h2>
      {{ session_info.uid }}

      <h2>-- session_info.username---</h2>
      {{ session_info.username }}

      <h2>-- session_info.name---</h2>
      {{ session_info.name }}

      <h2>-- context---</h2>
      {{ context }}
    </div>
  </div>
</template>

<script>
import api from '@/odoorpc'

// const baseURL = 'http://192.168.56.108:8069'
const baseURL = process.env.VUE_APP_BASE_API
const timeout = 50000
api.init({ baseURL, timeout })

export default {
  name: 'TestDataset',
  props: {},
  data() {
    return {
      session_info: {},
      context: {},

      fields_info: {},
      dataDict: {}
    }
  },
  computed: {},

  async created() {},
  methods: {
    format_selection(field) {
      const value = this.dataDict[field]
      const meta = this.fields_info[field] || {}
      if (meta.type !== 'selection') {
        return value
      }

      const get_label = v => {
        const elm = meta.selection.find(item => item[0] === v)
        return elm ? elm[1] : ''
      }

      return value ? get_label(value) : ''
    },

    async onclick() {
      const fields = [
        'name',
        'display_name',
        'type',
        'barcode',
        'default_code',
        'categ_id'
      ]

      const Model = await api.env.model('product.template', { fields })

      this.fields_info = Model._fields

      const result = await Model.search_read({
        domain: [],
        fields: ['name', 'display_name']
      })

      const res_id = result[0].id
      const res = await Model.read(res_id, { fields })
      console.log('result:', res)

      this.dataDict = res[0]
    },

    async onclickLogin() {
      console.log('click btn')
      const kwargs = { db: 'T1', login: 'admin', password: '123456' }
      const result = await api.web.session.authenticate(kwargs)
      console.log('result:', result)
      this.session_info = result
      this.context = api.web.session.context
    }
  }
}
</script>

<style scoped></style>
